16. Solution: Code Coverage

Solution: Code Coverage

ND079 JPND C3 L4 A12 Solution Code Coverage V2

TotalsWithDiscountCalculatorTest

Code coverage reveals that we have no unit tests checking the scenario when a user runs out of credit. We need to write a new unit test to handle this scenario:

    @Test
    void getTotalsWithDiscounts_userWithCredit_creditLessThanSubtotal() {
        TotalsWithDiscountCalculator calculator = new TotalsWithDiscountCalculator(new User("User with a lot of credit", UserType.REGULAR, 15.0));
        CartTotals totals = calculator.getTotals(List.of(new CartItem("Twenty dollar item", 20.0, 0)));
        assertEquals(new CartTotals(5.0, 0), totals);
    }

Note that you could also accomplish this by modifying your ArgumentsSource method, but that would involve adding additional requirements into our test for regular vs. platinum users. Keeping with our goal to keep simple tests that test specific requirements, it is better to write a new test than overload the existing test.